cmake_minimum_required(VERSION 3.0.2)
project(drive)

find_package(catkin REQUIRED COMPONENTS
  cv_bridge
  image_transport
  message_generation
  roscpp
  sensor_msgs
  std_msgs
)

option(CUDA_USE_STATIC_CUDA_RUNTIME OFF)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_BUILD_TYPE Debug)

find_package(CUDA REQUIRED)

set(CUDA_NVCC_PLAGS ${CUDA_NVCC_PLAGS};-std=c++11;-g;-G;-gencode;arch=compute_30;code=sm_30)

include_directories(/usr/include)
include_directories(/usr/include/glib-2.0)
include_directories(/usr/lib/glib-2.0/include)
# 区分嵌入式平台环境
include_directories(${PROJECT_SOURCE_DIR}/include)
if (CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64")
    message("embed_platform on")
    include_directories(/usr/local/cuda/targets/aarch64-linux/include)
    link_directories(/usr/local/cuda/targets/aarch64-linux/lib)
    link_directories(/usr/lib/aarch64-linux-gnu)    # 字体渲染库，libfreetype的位置
    include_directories(/usr/include/gstreamer-1.0)
    include_directories(/usr/lib/aarch64-linux-gnu/glib-2.0/include)
else()
    message("embed_platform off")
    include_directories(/usr/local/cuda/include)
    link_directories(/usr/local/cuda/lib64)
    link_directories(/usr/lib/x86_64-linux-gnu/)
    include_directories(/usr/lib/x86_64-linux-gnu/gstreamer-1.0)
    include_directories(/usr/lib/x86_64-linux-gnu/gstreamer-1.0/include)
    include_directories(/usr/lib/x86_64-linux-gnu/glib-2.0)
    include_directories(/usr/lib/x86_64-linux-gnu/glib-2.0/include)
endif()

# 为了防止对 #pragma 语句的报警，加上 -Wno-unknown-pragmas 设置
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wno-unknown-pragmas -Ofast -Wfatal-errors -D_MWAITXINTRIN_H_INCLUDED")

# 不明白工程里的问题，这里凡是使用了 CUDA代码，都要编译为动态链接库才能使用
cuda_add_library(yololayer SHARED src/yolov5/yololayer.cu)
cuda_add_library(face_similarity SHARED src/arcface/face_similarity.cu)

add_message_files(FILES BoundingBox.msg BoundingBoxes.msg FaceId.msg)
generate_messages(DEPENDENCIES std_msgs sensor_msgs)

catkin_package(
#  INCLUDE_DIRS include
#  LIBRARIES driver_hand
  CATKIN_DEPENDS cv_bridge image_transport message_generation roscpp sensor_msgs std_msgs
#  DEPENDS system_lib
)

include_directories(
# include
  ${catkin_INCLUDE_DIRS}
)

add_executable(Decoder src/decode.cpp)
target_link_libraries(Decoder ${catkin_LIBRARIES})
add_dependencies(Decoder ${PROJECT_NAME}_generate_messages_cpp)
add_executable(FaceId src/faceId.cpp
        src/utils/logging.h
        src/utils/utils.h
        src/yolov5/dataset.h src/yolov5/dataset.cpp
        src/data/results.h src/data/results.cpp
        src/yolov5/common.h src/yolov5/common.cpp
        src/yolov5/yololayer.h src/yolov5/yololayer.cu
        src/yolov5/yolov5.h src/yolov5/yolov5.cpp
	
	src/arcface/draw_mask.h src/arcface/draw_mask.cpp
        src/arcface/face_similarity.cu
        src/arcface/prelu.h src/arcface/prelu.cu
        src/arcface/arcface.h src/arcface/arcface.cpp

	src/pfld/pfld.h src/pfld/pfld.cpp
	    	src/pfld/face_gesture.h src/pfld/face_gesture.cpp
	    	src/font/CvxText.h src/font/CvxText.cpp
    		src/utils/cfg_params.h src/utils/cfg_params.cpp
     	 src/database/SqliteOp.h    src/database/SqliteOp.cpp
        )
target_link_libraries(FaceId ${catkin_LIBRARIES})
add_dependencies(FaceId ${PROJECT_NAME}_generate_messages_cpp)
add_executable(Analyzer src/analyze.cpp
        src/utils/logging.h
        src/utils/utils.h
        src/utils/dataset.h src/utils/dataset.cpp
        src/data/results.h src/data/results.cpp
        src/yolov5/common.h src/yolov5/common.cpp
        src/yolov5/yololayer.h src/yolov5/yololayer.cu
        src/yolov5/yolov5.h src/yolov5/yolov5.cpp

      	src/pfld/pfld.h src/pfld/pfld.cpp
	    	src/pfld/face_gesture.h src/pfld/face_gesture.cpp
	    	src/font/CvxText.h src/font/CvxText.cpp
    		src/utils/cfg_params.h src/utils/cfg_params.cpp
     	 src/database/SqliteOp.h    src/database/SqliteOp.cpp
  
     	 src/arcface/draw_mask.h src/arcface/draw_mask.cpp
     	 src/arcface/face_similarity.cu
    	 src/arcface/prelu.h src/arcface/prelu.cu
    	 src/arcface/arcface.h src/arcface/arcface.cpp
	
	)
target_link_libraries(Analyzer ${catkin_LIBRARIES})
add_dependencies(Analyzer ${PROJECT_NAME}_generate_messages_cpp)

add_executable(Renderer src/render.cpp
        src/font/CvxText.cpp src/font/CvxText.h
        src/arcface/draw_mask.h src/arcface/draw_mask.cpp)
target_link_libraries(Renderer ${catkin_LIBRARIES})
add_dependencies(Renderer ${PROJECT_NAME}_generate_messages_cpp)

find_package(OpenCV)
include_directories(OpenCV_INCLUDE_DIRS)
target_link_libraries(Decoder ${OpenCV_LIBS})
target_link_libraries(Analyzer ${OpenCV_LIBS})
target_link_libraries(Renderer ${OpenCV_LIBS})
target_link_libraries(FaceId ${OpenCV_LIBS})

target_link_libraries(Analyzer nvinfer)
target_link_libraries(Analyzer cudart)
target_link_libraries(Analyzer yololayer)

target_link_libraries(Analyzer face_similarity)

target_link_libraries(FaceId nvinfer)
target_link_libraries(FaceId cudart)
target_link_libraries(FaceId yololayer)

target_link_libraries(FaceId face_similarity)
include_directories(/usr/lib)
include_directories(/usr/lib/glib-2.0/include)
include_directories(/usr/lib/x86_64-linux-gnu -lgio-2.0)
target_link_libraries(Analyzer glib-2.0)
target_link_libraries(Analyzer gobject-2.0)
target_link_libraries(Analyzer gmodule-2.0)
target_link_libraries(Analyzer gstreamer-1.0)
target_link_libraries(Analyzer gstbase-1.0)
target_link_libraries(Analyzer gstapp-1.0)
target_link_libraries(Analyzer gstnet-1.0)

include_directories(/usr/include/freetype2)
target_link_libraries(Renderer freetype)
include_directories(/usr/include/libxml2)
#target_link_libraries(Analyzer libxml)
target_link_libraries(Analyzer "/usr/lib/aarch64-linux-gnu/libxml2.so.2")
target_link_libraries(Analyzer "/usr/lib/aarch64-linux-gnu/libfreetype.so.6")
target_link_libraries(Analyzer "/usr/lib/aarch64-linux-gnu/libsqlite3.so.0")

target_link_libraries(FaceId "/usr/lib/aarch64-linux-gnu/libxml2.so.2")
target_link_libraries(FaceId "/usr/lib/aarch64-linux-gnu/libfreetype.so.6")
target_link_libraries(FaceId "/usr/lib/aarch64-linux-gnu/libsqlite3.so.0")
# find_package (Threads)
target_link_libraries(Analyzer sqlite3)
# target_link_libraries (Analyzer ${CMAKE_THREAD_LIBS_INIT})

add_definitions(-O2 -pthread)
